class ActivityFoodUI extends BaseComp{
    private item_1:Item;
    private item_2:Item;
    private item_3:Item;
    private item_4:Item;
    private item_5:Item;
    private item_6:Item;
    private btn_get_1:BaseButton;
    private btn_get_2:BaseButton;

    constructor()
    {
        super('ActivityFoodView');
    }

    public init()
    {

    }

    public initUI(data)
    {
        this.data = data;
        let lunchInfo = this.data.lunchInfo;
        let dinnerInfo = this.data.dinnerInfo;
        this.initLunch(lunchInfo);
        this.initDinner(dinnerInfo);
    }

    public initLunch(lunchInfo)
    {
        let is_get = lunchInfo.is_get;
        let can_get = lunchInfo.can_get;
        let award:string = lunchInfo.award;
        if(is_get || !can_get)
        {
            let img:eui.Image = this.btn_get_1.getChildByName('btn_img') as eui.Image;
            ShaderUtil.ImgGray(img);
        }
        let awardArr = award.split(',');
        awardArr.forEach((value, i, arr)=>{
            let item:Item = this['item_'+(i+1)];
            if(item)
            {
                item.initUI(value);
            }
        })
    }

    public initDinner(dinnerInfo)
    {
        let is_get = dinnerInfo.is_get;
        let can_get = dinnerInfo.can_get;
        let award:string = dinnerInfo.award;
        if(is_get || !can_get)
        {
            let img:eui.Image = this.btn_get_2.getChildByName('btn_img') as eui.Image;
            ShaderUtil.ImgGray(img);
        }
        let awardArr = award.split(',');
        awardArr.forEach((value, i, arr)=>{
            let item:Item = this['item_'+(i+4)];
            if(item)
            {
                item.initUI(value);
            }
        })
    }

    public addEvent()
    {
        for(let i=1;i<=2;i++)
        {
            let btn:BaseButton = this['btn_get_'+i];
            btn.addTapHandler(this.getAward.bind(this, [i]), this);
        }
    }

    protected getAward(data)
    {
        let index = data[0];
        if(index == 1)
        {
            //午餐
            if(this.data.lunchInfo.is_get)
            {
                MessageUtil.tips('已经领取过了哦~');
            }
            else if(!this.data.lunchInfo.can_get)
            {
                MessageUtil.tips('还未到领取时间哦~');
            }
            else if(!this.data.lunchInfo.is_get && this.data.lunchInfo.can_get)
            {
                let parma = {
                    type:'lunch_award'
                }
                ActivityPrase.instance().getWelfare(parma, this.updateUI.bind(this, [1]), this);
            }
        }
        else{
            //晚餐
            if(this.data.dinnerInfo.is_get)
            {
                MessageUtil.tips('已经领取过了哦~');
            }
            else if(!this.data.dinnerInfo.can_get)
            {
                MessageUtil.tips('还未到领取时间哦~');
            }
            else if(!this.data.dinnerInfo.is_get && this.data.dinnerInfo.can_get)
            {
                let parma = {
                    type:'dinner_award'
                }
                ActivityPrase.instance().getWelfare(parma, this.updateUI.bind(this, [2]), this);
            }
        }
    }

    public updateUI(data)
    {
        let type = data[0];
        if(type == 1)
        {
            this.data.lunchInfo.is_get = true;
            let award = this.data.lunchInfo.award;
            let img:eui.Image = this.btn_get_1.getChildByName('btn_img') as eui.Image;
            ShaderUtil.ImgGray(img);
            RoleCache.instance().isGetLunchAward = true;
        }
        else
        {
            this.data.dinnerInfo.is_get = true;
            let award = this.data.dinnerInfo.award;
            let img:eui.Image = this.btn_get_2.getChildByName('btn_img') as eui.Image;
            ShaderUtil.ImgGray(img);
            RoleCache.instance().isGetDinnerAward = true; 
        }
        GameUtil.updateRedPoint(RedPointID.Activity);
    }

    public removeEvent()
    {
        for(let i=1;i<=2;i++)
        {
            let btn:BaseButton = this['btn_get_'+i];
            btn.removeTapHandler();
        }
    }

    public clear()
    {
        super.clear();

        for(let i=1;i<=6;i++)
        {
            let item:Item = <Item>this['item_'+i];
            item.remove();
            item = null;
        }
    }
}

window['ActivityFoodUI'] = ActivityFoodUI;